package com.gph.etrip;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;


import com.gph.etrip.timmy.test.AndroidSaxXMLMarkerParser;
import com.gph.etrip.timmy.test.Marker;
import com.gph.etrip.timmy.test.ViewAdapter;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;


public class TimmyTest extends Activity {

	/**
	 * @file
	 * Activity for presenting available Ad in an intractable list
	 * @author   Hui Zhou
	 * @version 2.0
	 */
		    ListView list;
		    ViewAdapter adapter;
//		    String[] mStrings;
		
		    ArrayList<Marker> markerList = new ArrayList<Marker>();
		    private String selectedMarker = null;
		    private String markerUrl= null;
		    
		    @Override
		    public void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        setContentView(R.layout.main);
		        
		        try {
					markerList = getMarkerList();

					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        
		        list=(ListView)findViewById(R.id.list);
		        //adapter=new ViewAdapter(this, mStrings,arraylist);
		        //adapter=new ViewAdapter(this,mStrings,markerList);
		        adapter=new ViewAdapter(this,markerList);
		        list.setAdapter(adapter);
		        System.out.println("list adapter");
		        
		        //Button b=(Button)findViewById(R.id.button1);
		        //b.setOnClickListener(listener);
		        final DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
		        	  
		    		  public void onClick(DialogInterface dialog, int which) {
		    		        switch (which){
		    		        case DialogInterface.BUTTON_POSITIVE:
		    		        	
		    		        	//Toast.makeText(getApplicationContext(), "11111", Toast.LENGTH_LONG).show();
		    		        	
//		    		    		Intent intent= new Intent();
//		    					intent.putExtra("testIntent", selectedMarker);
//		    					intent.setClass(RewardsListView.this,SingleMarkerView.class);
//		    					RewardsListView.this.startActivity(intent);
		    		            break;
		    		            
		    		        case DialogInterface.BUTTON_NEGATIVE:
		    		            //No button clicked
		    		            break;
		    		        }
		    		    }
		    		};
		    		list.setOnItemClickListener(new OnItemClickListener() {

						public void onItemClick(AdapterView<?> parent, View arg1,
								int position, long arg3) {
							selectedMarker = (String) adapter.getMarkerName(position);
							markerUrl = (String) adapter.getMarkerIcon(position);
//							int m//kerId =((Marker)adapter.getItem(posit//n)).getId();
//							Intent intent= new Intent();
//	  //					intent.putExtra("testIntent", selectedMarker);
///    					intent.putExtra("testIntent1", markerUrl);//////	    					intent.putExtra("markerId",markerId);
//	    					intent.s//Class(RewardsListView.this,SingleMarkerView.class);
////   					RewardsListView.this.startActivity(intent);
//							
							/*
				    		AlertDialog.Builder builder = new AlertDialog.Builder(parent.getContext());
				    		builder.setMessage("Marker Type?").setPositiveButton("Current Location", dialogClickListener)
				    		    .setNegativeButton("With Distance", dialogClickListener).show();	
			    		    */
						}
		        	  });
		    }
		    
		    @Override
		    public void onDestroy()
		    {
		        adapter.imageLoader.stopThread();
		        list.setAdapter(null);
		        super.onDestroy();
		    }
		    
		    public OnClickListener listener=new OnClickListener(){
		        public void onClick(View arg0) {
		            adapter.imageLoader.clearCache();
		            adapter.notifyDataSetChanged();
		        }
		    };
		    
		    /**
		     * Retrieve defined markers provided in markerconfig.xml, in the assets folder.
		     * @return ArrayList of markers found in the configuration file.
		     */
		    private ArrayList<Marker> getMarkerList() throws FileNotFoundException {
		    	AndroidSaxXMLMarkerParser markerParser;
				try {
					markerParser = new AndroidSaxXMLMarkerParser();

					return markerParser.parse(getAssets().open("markerconfig.xml"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    	return null; 

			}
		    
	
		

	
	
}